﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using public_dbOpt;

namespace prjEMR22_154
{
    public partial class PatientInfo_inquire_checked : Form
    {
        dbOPT dataOpt = new dbOPT();
        public static string strHspID;

        public PatientInfo_inquire_checked()
        {
            InitializeComponent();
        }


        private void PatientInfo_inquire_checked_Load(object sender, EventArgs e)   //初始化显示病人具体信息
        {
            //绑定科室下拉列表
            dataOpt.BindDropdownlist("departinfo", DepartName, 1);
            string sql = string.Format("select * from doctorinfo where DepartName = '{0}' ", DepartName.Text);
            dataOpt.BindDropdownlist_sql(sql, DoctorName, 1);

            strHspID = PatientInfo_inquire.strHSPID;
            sql = String.Format("select * from patientinfo where HspID = '{0}';", strHspID);
            DataSet ds = new DataSet();//创建数据集
            ds = dataOpt.GetTable(sql);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                // 访问数据集中的数据  
                DataRow row = ds.Tables[0].Rows[0]; // 获取第一行数据  
                InitBoxes(this, row);//初始化设置各个控件的初始值
            }

            CloseBoxes(this);
            label15.Text = "只读模式";
            b1.Visible = false;
            b2.Visible = false;
            b3.Visible = false;
            b4.Visible = false;
            b5.Visible = false;
            b6.Visible = false;
            b7.Visible = false;
            //MessageBox.Show(sql);
        }

        private void InitBoxes(Control parent, DataRow row)  //初始化设置各个控件的初始值
        {  
            foreach (Control ctrl in parent.Controls)  
            {  
                if (ctrl is TextBox)  
                {
                    TextBox textBox = (TextBox)ctrl;
                    string columnName = ctrl.Name == "Pa_Name" ? "Name" : ctrl.Name;  
  
                    // 检查列是否存在并尝试获取值  
                    if (row.Table.Columns.Contains(columnName))  
                    {  
                        object value = row[columnName];  
                        if (value != DBNull.Value) // 检查值是否为 DBNull  
                        {  
                            textBox.Text = value.ToString();  
                        }  
                    }  
                    // 如果列不存在，可以选择记录日志、抛出异常或执行其他操作  
                    // else { /* 处理列不存在的情况 */ }  
                }

                if (ctrl is ComboBox)
                {
                    ComboBox textBox = (ComboBox)ctrl;

                    if (row.Table.Columns.Contains(ctrl.Name))
                    {
                        object value = row[ctrl.Name];
                        if (value != DBNull.Value) // 检查值是否为 DBNull  
                        {
                            textBox.Text = value.ToString();
                        }
                    }
                    // 如果列不存在，可以选择记录日志、抛出异常或执行其他操作  
                    // else { /* 处理列不存在的情况 */ } 
                }
                // 递归遍历子控件  
                if (ctrl.Controls.Count > 0)  
                {
                    InitBoxes(ctrl, row);  
                } 
            }  

        }

         private void OpenExdit_Click(object sender, EventArgs e)//打开编辑
         {
             OpenBoxes(this);
             label15.Text = "编辑模式";
             b1.Visible = true;
             b2.Visible = true;
             b3.Visible = true;
             b4.Visible = true;
             b5.Visible = true;
             b6.Visible = true;
             b7.Visible = true;
         }

         private void CencelExdit_Click(object sender, EventArgs e)//取消编辑
         {
             CloseBoxes(this);
             label15.Text = "只读模式";
             b1.Visible = false;
             b2.Visible = false;
             b3.Visible = false;
             b4.Visible = false;
             b5.Visible = false;
             b6.Visible = false;
             b7.Visible = false;
         }

         private void CloseBoxes(Control parent)  //关闭编辑模式函数
         {
             foreach (Control ctrl in parent.Controls)
             {
                 if (ctrl is TextBox)
                 {
                     TextBox textBox = (TextBox)ctrl;
                     textBox.ReadOnly = true;
                 }

                 if (ctrl is ComboBox)
                 {
                     ComboBox textBox = (ComboBox)ctrl;
                     textBox.Enabled = false;
                 }
                 // 递归遍历子控件  
                 if (ctrl.Controls.Count > 0)
                 {
                     CloseBoxes(ctrl);
                 }
             }

         }

         private void OpenBoxes(Control parent)  //打开编辑模式函数
         {
             foreach (Control ctrl in parent.Controls)
             {
                 if (ctrl is TextBox)
                 {
                     TextBox textBox = (TextBox)ctrl;
                     textBox.ReadOnly = false;
                 }

                 if (ctrl is ComboBox)
                 {
                     ComboBox textBox = (ComboBox)ctrl;
                     textBox.Enabled = true;
                 }
                 // 递归遍历子控件  
                 if (ctrl.Controls.Count > 0)
                 {
                     OpenBoxes(ctrl);
                 }
             }

         }

         private void Save_Click(object sender, EventArgs e)    //保存
         {
             if (Pa_Name.Text != "" && Gender.Text != "" && Nation.Text != "" && HspID.Text != "" && DoctorName.Text != ""
                && DepartName.Text != "" && InHspDiagnose.Text != "")
             {
                 foreach (Control ctrl in this.Controls)
                 {
                     if (ctrl is TextBox)
                     {
                         TextBox textbox = (TextBox)ctrl;
                         string text_name = textbox.Name == "Pa_Name" ? "Name" : textbox.Name;
                         string sql = String.Format("UPDATE patientinfo SET {0} = '{1}' where HspID = '{2}'", text_name, textbox.Text, strHspID);   //set后跟的属性不能加''
                         dataOpt.OperateData(sql);
                     }

                     if (ctrl is ComboBox)
                     {
                         ComboBox textbox = (ComboBox)ctrl;
                         string sql = String.Format("UPDATE patientinfo SET {0} = '{1}' where HspID = '{2}'", textbox.Name, textbox.Text, strHspID);
                         dataOpt.OperateData(sql);
                     }
                 }
                 MessageBox.Show("保存成功");
                 strHspID = HspID.Text;
             }
             else
             {
                 MessageBox.Show("必填项有空项，请重新输入");
             }
         }
            
         private void Cencel_Click(object sender, EventArgs e)  //取消,取消此前的操作
         {
             string sql = String.Format("select * from patientinfo where HspID = '{0}';", strHspID);
             DataSet ds = new DataSet();//创建数据集
             ds = dataOpt.GetTable(sql);

             if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
             {
                 // 访问数据集中的数据  
                 DataRow row = ds.Tables[0].Rows[0]; // 获取第一行数据  
                 InitBoxes(this, row);//初始化设置各个控件的初始值
             }
         }

         private void Delete_Click(object sender, EventArgs e)  //删除操作
         {
             PatientInfo_inquire_checkedDE a = new PatientInfo_inquire_checkedDE();  //展开病人数据
             a.ShowDialog();
         }

         private void MRID_TextChanged(object sender, EventArgs e)
         {

         }

         private void 查看病历ToolStripMenuItem_Click(object sender, EventArgs e)
         {
             MR_contrl a = new MR_contrl();
             a.Show();
         }

         private void DepartName_SelectedIndexChanged(object sender, EventArgs e)
         {
             DoctorName.Items.Clear();
             string sql = string.Format("select * from doctorinfo where DepartName = '{0}' ", DepartName.Text);
             dataOpt.BindDropdownlist_sql(sql, DoctorName, 1);
         }

         private void 查看用药情况ToolStripMenuItem_Click(object sender, EventArgs e)
         {
             Pa_Md a = new Pa_Md();
             a.ShowDialog();
         }

         private void 检查项目ToolStripMenuItem_Click(object sender, EventArgs e)
         {
             Pa_Ins a = new Pa_Ins();
             a.ShowDialog();
         }



    }
}
